package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 查询商机列表
     */
    @GetMapping
    public Result list(BusinessDto businessDto){
        log.info("查询商机列表: {}",businessDto);
        PageResult<Business> pageResult = businessService.listBusiness(businessDto);
        return Result.success(pageResult);

    }
    /**
     * 添加商机
     */
    @PostMapping
    public Result add(@RequestBody Business business){
        log.info("添加商机: {}",business);
        business.setStatus(1);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }
    /**
     * 修改商机,分配商机
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId ,@PathVariable Integer userId){
        log.info("修改商机: {},{}",businessId,userId);
        Business business = new Business();
        business.setUserId(userId);
        business.setId(businessId);
        business.setStatus(2);//待跟进
        business.setUpdateTime(LocalDateTime.now());

        businessService.updateById(business);
        return Result.success();
    }

    /**
     * 根据id查询商机
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询id：{}",id);
        Business business = businessService.selectById(id);
        return Result.success(business);
    }

    /**
     * 跟进商机
     * 1.根据id更新商机基本信息
     * 2.添加商机跟进记录
     */
    @PutMapping
    public Result trackBusiness(@RequestBody Business business){
        log.info("跟进商机: {}",business);
        businessService.trackBusiness(business);
        return Result.success();
    }
    /**
     * 公海池列表
     */
    @GetMapping("/pool")
    public Result poolList(BusinessDto businessDto){
        log.info("公海池: {}",businessDto);
        PageResult<Business> pageResult = businessService.poolList(businessDto);
        return Result.success(pageResult);
    }
    /**
     * 踢回公海
     */
    @PutMapping("/back/{id}")
    public Result backPool(@PathVariable Integer id){
        log.info("踢回公海: {}",id);
        businessService.backPool(id);
        return Result.success();
    }

    /**
     * 转客户
     */
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        log.info("转客户: {}",id);
        businessService.toCustomer(id);
        return Result.success();
    }


}
